{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "\n",
    "from scipy.misc import imsave\n",
    "import numpy as np\n",
    "import time\n",
    "from keras.models import model_from_json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from keras import backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "img_width = 106\n",
    "img_height = 106"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def normalize(x):\n",
    "    # utility function to normalize a tensor by its L2 norm\n",
    "    return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# load json and create model\n",
    "json_file = open('model.json', 'r')\n",
    "loaded_model_json = json_file.read()\n",
    "json_file.close()\n",
    "loaded_model = model_from_json(loaded_model_json)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "____________________________________________________________________________________________________\n",
      "Layer (type)                     Output Shape          Param #     Connected to                     \n",
      "====================================================================================================\n",
      "input_1 (InputLayer)             (None, 224, 224, 3)   0                                            \n",
      "____________________________________________________________________________________________________\n",
      "input_2 (InputLayer)             (None, 224, 224, 3)   0                                            \n",
      "____________________________________________________________________________________________________\n",
      "sequential_1 (Sequential)        (None, 256)           44386720    input_1[0][0]                    \n",
      "                                                                   input_2[0][0]                    \n",
      "____________________________________________________________________________________________________\n",
      "lambda_1 (Lambda)                (None, 1)             0           sequential_1[1][0]               \n",
      "                                                                   sequential_1[2][0]               \n",
      "====================================================================================================\n",
      "Total params: 44,386,720\n",
      "Trainable params: 44,386,400\n",
      "Non-trainable params: 320\n",
      "____________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "loaded_model.summary()\n",
    "input_img = loaded_model.input\n",
    "layer_dict = dict([(layer.name, layer) for layer in loaded_model.layers[1:]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "print(len(loaded_model.layers)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<keras.layers.convolutional.Convolution2D at 0x10ffc0d10>,\n",
       " <keras.layers.core.Activation at 0x11006e1d0>,\n",
       " <keras.layers.convolutional.Convolution2D at 0x11006e610>,\n",
       " <keras.layers.normalization.BatchNormalization at 0x110089e90>,\n",
       " <keras.layers.core.Activation at 0x1101739d0>,\n",
       " <keras.layers.pooling.MaxPooling2D at 0x110192f10>,\n",
       " <keras.layers.core.Dropout at 0x1100defd0>,\n",
       " <keras.layers.convolutional.Convolution2D at 0x1101a4c50>,\n",
       " <keras.layers.normalization.BatchNormalization at 0x11020d890>,\n",
       " <keras.layers.core.Activation at 0x11024a050>,\n",
       " <keras.layers.convolutional.Convolution2D at 0x1102ca790>,\n",
       " <keras.layers.normalization.BatchNormalization at 0x110337ed0>,\n",
       " <keras.layers.core.Activation at 0x1103e6e10>,\n",
       " <keras.layers.pooling.AveragePooling2D at 0x110392310>,\n",
       " <keras.layers.core.Dropout at 0x110365e50>,\n",
       " <keras.layers.core.Flatten at 0x110437710>,\n",
       " <keras.layers.core.Dense at 0x110447590>]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loaded_model.layers[2].layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from keras.models import Sequential, Model\n",
    "from keras.layers import Dense, Dropout, Input, Lambda, Activation, Flatten\n",
    "from keras.layers import Convolution2D, MaxPooling2D,BatchNormalization, AveragePooling2D\n",
    "from keras.regularizers import l2, activity_l2\n",
    "from keras.optimizers import RMSprop,Adagrad\n",
    "input_shape = (224,224,3)\n",
    "seq = Sequential()\n",
    "seq.add(Convolution2D(32, 5, 5, border_mode='same',\n",
    "                input_shape=input_shape, weights=loaded_model.layers[2].layers[0].get_weights()))\n",
    "seq.add(Activation('relu'))\n",
    "seq.add(Convolution2D(32, 5, 5,weights=loaded_model.layers[2].layers[2].get_weights()))\n",
    "seq.add(BatchNormalization(weights=loaded_model.layers[2].layers[3].get_weights()))\n",
    "seq.add(Activation('relu'))\n",
    "seq.add(MaxPooling2D(pool_size=(5, 5), strides=(2, 2)))\n",
    "seq.add(Dropout(0.25))\n",
    "seq.add(Convolution2D(64, 3, 3,weights=loaded_model.layers[2].layers[7].get_weights()))\n",
    "seq.add(BatchNormalization(weights=loaded_model.layers[2].layers[8].get_weights()))\n",
    "seq.add(Activation('relu'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from datasets.tidr import load_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# the data, shuffled and split between train and test sets\n",
    "DistortImg, DistortLabel, RefImg, RefLabel, ScoreLabel = load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def create_compare(x_train, x_ref):\n",
    "    pairs = []\n",
    "    labels = []\n",
    "    for i in xrange(25):\n",
    "        for j in xrange(120):\n",
    "            pairs += [[x_train[120*i+j], x_ref[i]]]\n",
    "\n",
    "    return np.array(pairs)\n",
    "\n",
    "all_pairs = create_compare(DistortImg, RefImg)\n",
    "all_pairs =  all_pairs.astype(\"float32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "visual = seq.predict(all_pairs[2000,:,:,:,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 106, 106, 64)"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "visual.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  3.40917301,   3.72002172,   3.6272018 , ...,   3.56616497,\n",
       "          3.4779191 ,   4.22800064],\n",
       "       [  3.39430165,   3.65880394,   3.57583857, ...,   3.81290579,\n",
       "          4.19020128,   5.12780905],\n",
       "       [  3.75757909,   3.91607451,   3.95146918, ...,   4.3578043 ,\n",
       "          4.26509285,   5.56821394],\n",
       "       ..., \n",
       "       [ 31.95001602,  31.13913345,  32.5921936 , ...,  24.63685417,\n",
       "         25.55436897,  32.47183228],\n",
       "       [ 32.2490654 ,  29.07502556,  30.61402893, ...,  23.96595001,\n",
       "         21.82162094,  30.84044266],\n",
       "       [ 32.85877991,  30.67744064,  29.07406998, ...,  27.64712715,\n",
       "         20.81101227,  27.03387451]], dtype=float32)"
      ]
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "visual[1,:,:,5] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "n1 = 8\n",
    "n2 = 8\n",
    "margin = 5\n",
    "width = n2 * img_width + (n2 - 1) * margin\n",
    "height = n1 * img_height + (n1 - 1) * margin\n",
    "stitched_filters = np.zeros((width, height))\n",
    "visual = visual +50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(n2):\n",
    "    for j in range(n1):\n",
    "        img  = visual[0,:,:, j * n2 + i]\n",
    "        stitched_filters[(img_width + margin) * i: (img_width + margin) * i + img_width,\n",
    "                         (img_height + margin) * j: (img_height + margin) * j + img_height] = img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "img = Image.fromarray(stitched_filters)\n",
    "\n",
    "img.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
